package com.whfc.base.service.impl;

import com.whfc.base.dao.AppWarnFreqConfigMapper;
import com.whfc.base.dto.AppWarnFreqConfigDTO;
import com.whfc.base.entity.AppWarnFreqConfig;
import com.whfc.base.enums.AppModuleType;
import com.whfc.base.enums.AppWarnConfigType;
import com.whfc.base.manager.AppAlarmConfigManager;
import com.whfc.base.redis.WarnConfigRedisDao;
import com.whfc.base.service.WarnConfigService;
import com.whfc.common.exception.BizException;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2020-12-10
 */
@DubboService(interfaceClass = WarnConfigService.class, version = "1.0.0")
public class WarnConfigServiceImpl implements WarnConfigService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WarnConfigRedisDao warnConfigRedisDao;

    @Autowired
    private AppAlarmConfigManager appAlarmConfigManager;

    @Autowired
    private AppWarnFreqConfigMapper appWarnFreqConfigMapper;

    @Override
    public boolean checkAlarmFrequency(Integer deptId, Integer moduleType, Integer ruleType, String objectId, Date triggerTime) {
        logger.info("检测报警频率服务,deptId:{},moduleType:{},ruleType:{},objectId:{},time:{}",
                deptId, moduleType, ruleType, objectId, triggerTime);
        return appAlarmConfigManager.checkAlarmFrequency(deptId, moduleType, ruleType, objectId, triggerTime);
    }

    @Override
    public void refreshWarnConfig(Integer deptId, Integer warnModuleType, Integer type) {
        logger.info("删除报警配置缓存服务,deptId:{},warnModuleType:{},type:{}", deptId, warnModuleType, type);
        warnConfigRedisDao.delWarnConfig();
    }

    @Override
    public List<AppWarnFreqConfigDTO> getFreqConfigList(Integer deptId, AppModuleType moduleType) throws BizException {
        return appWarnFreqConfigMapper.selectFreqConfigList(deptId, moduleType.value());
    }

    @Override
    public void freqConfigure(Integer deptId, AppModuleType moduleType, List<AppWarnFreqConfigDTO> list) throws BizException {
        //清除所有针对报警类型的配置
        appWarnFreqConfigMapper.logicDel(deptId, moduleType.value(), AppWarnConfigType.MODULE_DEPT_RULE.value());
        for (AppWarnFreqConfigDTO appWarnFreqConfigDTO : list) {
            Integer type = appWarnFreqConfigDTO.getType();
            Integer ruleType = appWarnFreqConfigDTO.getRuleType();
            Integer module = moduleType.value();
            AppWarnFreqConfig freqConfig = appWarnFreqConfigMapper.selectByDeptIdAndModuleType(deptId, module, type, ruleType);
            if (freqConfig == null) {
                //新增
                freqConfig = new AppWarnFreqConfig();
            }
            freqConfig.setDeptId(deptId);
            freqConfig.setType(type);
            freqConfig.setModuleType(module);
            freqConfig.setRuleType(ruleType);
            freqConfig.setFrequencyLimitDay(appWarnFreqConfigDTO.getFrequencyLimitDay());
            freqConfig.setFrequencyLimitHour(appWarnFreqConfigDTO.getFrequencyLimitHour());
            freqConfig.setFrequencyLimitInterval(appWarnFreqConfigDTO.getFrequencyLimitInterval());
            if (freqConfig.getId() == null) {
                //新增
                appWarnFreqConfigMapper.insertSelective(freqConfig);
            } else {
                //修改
                appWarnFreqConfigMapper.updateByPrimaryKeySelective(freqConfig);
            }
        }

    }
}
